<?php
declare (strict_types = 1);

namespace app\controller;

use app\service\GoodsService;
use think\facade\Validate;
use think\Request;
use app\common\BaseController;

class GoodsController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', 10);
      $start_date = $request->param('start_date', '');
      $end_date = $request->param('end_date', '');
      $conditions = [];
      if ($start_date) {
        $conditions[] = ['date', $start_date, '>='];
      }
      if ($end_date) {
        $conditions[] = ['date', $end_date, '<='];
      }
      $list = GoodsService::getInstance()->getGoodsList($conditions,$page,$pageSize);
      $pager = GoodsService::getInstance()->getGoodsPager($conditions,$page,$pageSize);
      return $this->send200ResponseWithPager($list, $pager);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $house_id = $request->param('house_id', '');
      if (!$house_id) {
        return $this->send400Response('缺少house_in');
      }
      $data = $request->param('data', []);

      $validate = Validate::rule([
        'date' => 'require',
        'newamoumt' => 'require',
        'price' => 'require',
        'total' => 'require',
        'paid' => 'require',
        'js' => 'require',
        'gys' => 'require'
      ]);
      if (!$validate->check($data)) {
        return $this->send400Response($validate->getError());
      }
      $res = GoodsService::getInstance()->createGoods($house_id, $data);
      if (!$res['status']) {
        return $this->send400Response($res['msg']);
      }
      return $this->send200Response('ok');


    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      $data = GoodsService::getModel()->find($id);
      return $this->send200Response($data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
      $data = $request->param('data', []);
      $res = GoodsService::getInstance()->updateGoods($id, $data);
      if (!$res['status']) {
        return $this->send400Response($res['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
      $res = GoodsService::getInstance()->deleteGoods($id);
      if (!$res['status']) {
        return $this->send400Response($res['msg']);
      }
      return $this->send200Response('ok');
    }

  /**
   * 批量保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function saveMulti(Request $request)
  {
    $goods = $request->param('goods', []);
    $isAdd = $request->param('isAdd', 0);
    // 是否强制添加
    $res = GoodsService::getInstance()->createGoodsMulti($goods, $isAdd);
    if (!$res['status']) {
      return $this->send400Response($res['msg']);
    }
    return $this->send200Response($res['msg']);
  }

  /**
   * 确认入库操作
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function confirm($id)
  {
    $res = GoodsService::getInstance()->confirmGoods($id);
    if (!$res['status']) {
      return $this->send400Response($res['msg']);
    }
    return $this->send200Response('ok');
  }

  /**
   * 撤销入库操作
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function cancel($id)
  {
    $res = GoodsService::getInstance()->cancelGoods($id);
    if (!$res['status']) {
      return $this->send400Response($res['msg']);
    }
    return $this->send200Response('ok');
  }
}
